package com.example.demo_pc.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo_pc.controller.TestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description :
 * @Author : wanghao
 * @Date: 2020-10-10 10:55
 */
public class FileUtils {
    private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);

    /**
     * 根绝规则查找符合文本内容
     * @param rule 规则
     * @param text 文本
     * @return
     */
    public static String getContent(String rule,String text) {
        Pattern pattern = Pattern.compile(rule);
        Matcher matcher = pattern.matcher(text);
        if(matcher.find()) {
            String group=matcher.group();
            return group;
        }
        return null;
    }



    public static String numToString(String num,int chapterNum){
        int value=num.length();
        int lenght=chapterNum-value;

        for (int i=0;i<lenght;i++){
            num="0"+num;
        }
        return num;
    }

    public static List<String> getFiles(String path) {
        List<String> files = new ArrayList<>();
        File file = new File(path);
        File[] tempList = file.listFiles();

        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isFile()) {
                //文件名，不包含路径
            }
            if (tempList[i].isDirectory()) {
                //这里就不递归了，

                files.add(tempList[i].toString());
            }
        }
        return files;
    }

    public static List<String> testingNullFolder(String path) {
        List<String> nullFolder = new ArrayList<>();
        File file = new File(path);
        File[] tempList = file.listFiles();

        for (int i = 0; i < tempList.length; i++) {
            if (tempList[i].isDirectory()) {
                //这里就不递归了，

                File file1 = new File(tempList[i].toString());
                File[] listFiles = file1.listFiles();
                if(listFiles ==null || listFiles.length ==0){
                    //文件夹下没有文件
                    nullFolder.add(tempList[i].toString());
                }else {
                    List<String> strings = testingNullFolder(tempList[i].toString());
                    nullFolder.addAll(strings);
                }
            }
        }
        return nullFolder;
    }

    public static void writer(List<String> list,String path)  {
        try {
            File file = new File(path);
            //将list写入文件
            Writer out = new FileWriter(file);
            BufferedWriter bw= new BufferedWriter(out);
            bw.write(JSON.toJSONString(list));
            bw.newLine();
            bw.flush();
            bw.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

//    public static void main(String[] args) {
//        List<String> stringList= Arrays.asList("1","2","3");
//        writeContent(stringList,"D:\\test\\test1.txt","23333",null);
//        writer(stringList,"D:\\test\\test2.txt");
//    }
    public static String readFileContent(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        StringBuffer sbf = new StringBuffer();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                sbf.append(tempStr);
            }
            reader.close();
            String s = sbf.toString();

            return s;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }


    public static void writeContent(List<String> list,String path,String page,String formatDate) {
        try {
            File file=new File(path);
            if (!file.getParentFile().exists()){
                file.getParentFile().mkdirs();
            }
            FileWriter fw=new FileWriter(path,true);
            if (formatDate!=null && !"".equals(formatDate)){
                fw.write("\n\n\n"+formatDate);
            }
            fw.write("\n"+page);
            for (int i=0;i<list.size();i++){
                fw.write("\n"+(i+1)+"."+list.get(i));
            }
            fw.close();
        } catch (Exception e) {
            logger.info("输出时错误",e);
        }
    }

    public static String formatDate(Date date) throws Exception {
        SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sDateFormat.format(date);
        return format;
    }



}
